package com.sunelec.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Author : zml
 * Date   : 2018/3/3
 * Time   : 12:50
 * 类说明  : 普通方法的工具包
 */
public class MethodUtil {
    private static int PAGESIZE=10000;
    /**
     * 获取日期
     * @return 日期字符串
     */
    public static String getData(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 获取时间戳字符串
     */
    public static String getTimer(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }

    /**
     * 获取随机数字符串
     */
    public static String getRandom(int a){
        Random random = new Random();
        StringBuilder buffer = new StringBuilder("");
        for (int i = 0;i<a;i++){
            int b = random.nextInt(10);
            buffer.append(b).append("");
        }
        return buffer.toString();
    }
    /**
     * 获取文件路径并将文件写入磁盘
     */
    public static void getFilePath(HttpServletRequest request, MultipartFile[] files, StringBuilder sb) throws IOException {
        for(MultipartFile f:files){
            if(f.getOriginalFilename().length()>0){
                String originalFilename = f.getOriginalFilename();
                String realURL = request.getServletContext().getRealPath("");
                String newFilename = MethodUtil.getTimer()+MethodUtil.getRandom(4)+originalFilename.substring(originalFilename.lastIndexOf("."));
                File newFile = new File(realURL+"/static/image/"+newFilename);
                f.transferTo(newFile);
                sb.append("/sunelec/static/image/").append(newFilename).append("&&");
            }
        }
        while (sb.toString().endsWith("&&")){
            sb.replace(sb.length()-2,sb.length(),"");
        }
    }
    /**
     * 删除文件
     */
    public static void deleteFileFromDisk(HttpServletRequest request,String path){
        String realURL = request.getServletContext().getRealPath("");
        if(path != null && !path.equals("")){
            String[] paths = path.split("&&");
            for(String mypath:paths){
                if(mypath.startsWith("/sunelec")){
                    mypath = mypath.substring(6);
                }
                File file = new File(realURL+mypath);
                if (file.exists()){
                    file.delete();
                }
            }
        }

    }
    public static int getPAGESIZE(){
        return PAGESIZE;
    }

    /**
     * 获取文件路径并将文件写入磁盘
     */
    public static void uploadFile(HttpServletRequest request, MultipartFile[] files, StringBuilder sb) throws IOException {
        for(MultipartFile f:files){
            if(f.getOriginalFilename().length()>0){
                String originalFilename = f.getOriginalFilename();
                String realURL = request.getServletContext().getRealPath("");
                String newFilename = MethodUtil.getTimer()+MethodUtil.getRandom(4)+originalFilename.substring(originalFilename.lastIndexOf("."));
                File newFile = new File(realURL+"/static/file/"+newFilename);
                f.transferTo(newFile);
                sb.append("/sunelec/static/file/").append(newFilename).append("&&");
            }
        }
        while (sb.toString().endsWith("&&")){
            sb.replace(sb.length()-2,sb.length(),"");
        }
    }

}
